package com.hs.auth

import android.app.Dialog
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModelProvider
import com.hs.auth.R.color
import com.hs.auth.R.mipmap
import com.hs.auth.databinding.ModAuthLoginFragmentBinding
import com.hs.auth.viewmodel.LoginViewModel
import com.hs.common.dto.SysUserDto
import com.hs.common.provider.SysUserProvider
import com.hs.framework.toast.TipsToast
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LoginDialogFragment constructor(val authListener: AuthListener) : DialogFragment() {
    // TODO: 发布时清理掉界面写死的账号密码 
    lateinit var loginViewModel: LoginViewModel;
    private var _binding: ModAuthLoginFragmentBinding? = null
    private val binding get() = _binding!!
//    private var authListener: AuthListener? = null
    /**
     *  授权模式 ： 1 = 人脸  2 = 指纹  3 = 账号密码
     */
    private var authType: Int = 1
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.window?.setFlags(
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        )
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
        return dialog
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = ModAuthLoginFragmentBinding.inflate(inflater, container, false)
        initView()
        return binding.root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        loginViewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
        loginViewModel.openUsbDev()
        observeData()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        loginViewModel.stopUsbDev()
        _binding = null
    }

    /**
     * 初始化view
     */
    private fun initView() {
        binding.bgBorderImg.setImageResource(mipmap.mod_auth_au_bg_1)
        binding.faceFingerBorder.visibility = View.VISIBLE
        binding.icFpBg.visibility = View.GONE
        binding.faceTV.setTextColor(requireActivity().getColor(color.auth_white))
        binding.fingerTv.setTextColor(requireActivity().getColor(color.auth_color_333))
        binding.pswTv.setTextColor(requireActivity().getColor(color.auth_color_333))

        // 切换人脸登录
        binding.faceTV.setOnClickListener {
            binding.bgBorderImg.setImageResource(mipmap.mod_auth_au_bg_1)
            binding.faceTV.setTextColor(requireActivity().getColor(color.auth_white))
            binding.fingerTv.setTextColor(requireActivity().getColor(color.auth_color_333))
            binding.pswTv.setTextColor(requireActivity().getColor(color.auth_color_333))
            binding.icFpBg.visibility = View.GONE
            binding.faceFingerBorder.visibility = View.VISIBLE
            binding.tipMsgTv.visibility = View.VISIBLE
            binding.userPswLayout.visibility = View.GONE
            binding.tipMsgTv.text = "请正对摄像头"
            authType = 1
            loginViewModel.stopUsb()
        }

        // 切换指纹登录
        binding.fingerTv.setOnClickListener {
            binding.bgBorderImg.setImageResource(mipmap.mod_auth_au_bg_2)
            binding.faceTV.setTextColor(requireActivity().getColor(color.auth_color_333))
            binding.fingerTv.setTextColor(requireActivity().getColor(color.auth_white))
            binding.pswTv.setTextColor(requireActivity().getColor(color.auth_color_333))
            binding.icFpBg.visibility = View.VISIBLE
            binding.faceFingerBorder.visibility = View.VISIBLE
            binding.tipMsgTv.visibility = View.VISIBLE
            binding.userPswLayout.visibility = View.GONE
            binding.tipMsgTv.text = "请按指纹"
            authType = 2
            loginViewModel.findByFingerId()
        }

        // 切换账号密码登录
        binding.pswTv.setOnClickListener {
            binding.icFpBg.visibility = View.GONE
            binding.faceFingerBorder.visibility = View.GONE
            binding.bgBorderImg.setImageResource(mipmap.mod_auth_au_bg_3)
            binding.faceTV.setTextColor(requireActivity().getColor(color.auth_color_333))
            binding.fingerTv.setTextColor(requireActivity().getColor(color.auth_color_333))
            binding.pswTv.setTextColor(requireActivity().getColor(color.auth_white))
            binding.tipMsgTv.visibility = View.GONE
            binding.userPswLayout.visibility = View.VISIBLE
            binding.tipMsgTv.text = ""
            authType = 3
            loginViewModel.stopUsb()
        }

        // 账号密码登录去
        binding.gotoLoginTv.setOnClickListener {
            var account = binding.accountEd.text.toString()
            var password = binding.pswEd.text.toString()
            if (TextUtils.isEmpty(account)) {
                TipsToast.showTips("请输入账号")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(password)) {
                TipsToast.showTips("请输入密码")
                return@setOnClickListener
            }
            loginViewModel.loginByAccount(account, password)
        }
    }

    /**
     * 观察数据
     */
    private fun observeData() {
        loginViewModel.liveData.observe(this) { user ->
            run {
                println("${user.userName}")
                authListener?.onAuthResult(user)// TODO:  抓拍图
                // 更新用户信息
                SysUserProvider.updateUser(user)
                dismiss()
            }
        }

        loginViewModel.errorData.observe(this) { msg ->
            run {
                binding.tipMsgTv.visibility = View.VISIBLE
                binding.tipMsgTv.text = msg
            }
        }
    }


    companion object {
        fun show(fragmentManager: FragmentManager, authListener: AuthListener) {
            val fragment = LoginDialogFragment(authListener)
            fragment.show(fragmentManager, LoginDialogFragment::class.java.simpleName)
        }
    }

    /**
     *
     */
    public interface AuthListener {
        fun onAuthResult(sysUserDto: SysUserDto)
    }


}
